/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$('document').ready(function() {
    if ($('#profile_form').length > 0) {
        $('#profile_update_btn').click(function() {
            return submit();
        });
    }
    if ($('#profile_gender').length > 0 && $('#gender_in_session').length > 0) {
    	$('#profile_gender').val($('#gender_in_session').val());
    }
    if ($('#profile_is_single').length > 0 && $('#is_single_in_session').length > 0) {
    	$('#profile_is_single').val($('#is_single_in_session').val());
    }    
    $('#profile_constel')
        .focus(function() {
            $(this).select();
        })
        .mouseup(function(e) {
            e.preventDefault();
        })
        .popover({
            trigger: 'focus',
            placement: 'right',
            html: 'true',
            template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content constel_div"><p></p></div></div></div>'
        });
    // 'click' fail in IE, use 'mousedown' instead
    $(document).on('mousedown', 'td.constel', function() {
        $('#profile_constel').val($(this).text());
    });   
});

// 检查各字段是否填写正确
function check_info() {
    var ifChecked = true;  
    
    clean_error_span();
    
    if ( $('#profile_nickname').val() === "" ) {
        $('#profile_nickname').after('<span class=\'help-inline\'>请您输入昵称！</span>');
        $('div.control-group:has(#profile_nickname)').addClass('error');        
        ifChecked = false;
    }
    
    if ((constel_error = check_constel()) != '') {
        $('#profile_constel').after('<span class=\'help-inline\'>'+constel_error+'</span>');
        $('div.control-group:has(#profile_constel)').addClass('error');        
        ifChecked = false;        
    }
    
    return ifChecked;
}

function submit() {
    if ( check_info() ) {
        $.post("profile/update_profile", {
            nickname: $('#profile_nickname').val(),
            year_of_birth:  $('#profile_year_of_birth').val(),
            living_place: $('#profile_living_place').val(),
            constel: constel2value($('#profile_constel').val()),
            is_single: $('#profile_is_single').val(),
            biography: $('#profile_biography').val(),
			email_show: $('#profile_email_show').val(),
            qq_show: $('#profile_qq_show').val()
        }, function(data, textStatus) {
        	if(textStatus=="timeout") {
        		$('#message_show').nextAll('span').remove();
                $('#message_show').after('<span class=\'help-inline\'>请求超时，请重试</span>');
        	}
        	else if(data.code == -1) {
            	location.href = (window.EB_DEV ? "/ourplayground" : "");
            }
            else if(data.code == -2) {
                $('#profile_year_of_birth').after('<span class=\'help-inline\'>出生年份格式有误！</span>');
                $('div.control-group:has(#profile_year_of_birth)').addClass('error');            	
            }
            else if(data.code == -3) {
            	$('#message_show').nextAll('span').remove();
                $('#message_show').after('<span class=\'help-inline\'>输入的个人资料的格式有误或是超过最大长度，请修改后重试</span>');
            }
			else if(data.code == -4) {
            	$('#message_show').nextAll('span').remove();
                $('#message_show').after('<span class=\'help-inline\'>输入的邮箱格式有误！</span>');
            }
			else if(data.code == -5) {
            	$('#message_show').nextAll('span').remove();
                $('#message_show').after('<span class=\'help-inline\'>输入的qq号格式有误！</span>');
            }
            else if(data.code == 1) {
                $('#message_show').nextAll('span').remove();
                $('#message_show').after('<span class=\'help-inline\'>更新基本资料成功</span>');
            }
        }, "json");
    }
    return false;
}

function clean_error_span() {
    $('#profile_nickname').nextAll('span').remove();
    $('div.control-group:has(#profile_nickname)').removeClass('error');
    $('#profile_constel').nextAll('span').remove();
    $('div.control-group:has(#profile_constel)').removeClass('error');
    $('#profile_year_of_birth').nextAll('span').remove();
    $('div.control-group:has(#profile_year_of_birth)').removeClass('error');
	$('#profile_email_show').nextAll('span').remove();
	$('#profile_qq_show').nextAll('span').remove();
}

function check_constel() {
    var constel = $('#profile_constel').val();
    if (constel2value(constel) === -1) {
        return '请输入正确的星座！';
    } else {
        return '';
    }
}

function constel2value(constel) {
    switch (constel) {
        case '摩羯座':
            return 1;
        case '水瓶座':
            return 2;
        case '双鱼座':
            return 3;
        case '白羊座':
            return 4;
        case '金牛座':
            return 5;
        case '双子座':
            return 6;
        case '巨蟹座':
            return 7;
        case '狮子座':
            return 8;
        case '处女座':
            return 9;
        case '天秤座':
            return 10;
        case '天蝎座':
            return 11;
        case '射手座':
            return 12;
        case '':
            return 0;
        default:
            return -1;
    }
}